package cn.afactory.tygz.ui.base.views.search;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.ui.base.entity.LoganThinkWordEntity;
import cn.afactory.tygz.ui.base.utils.ViewUtils;

/**
 * Created by wangyouguo on 15/5/21.
 */
public class SearchView extends LinearLayout {
    private EditText mSearchContent;
    private ImageView mContentClearIcon;
    private TextView mCancelBtn;
    private ViewGroup mSearchContentView;
    private OnSearchViewClickListener mListener;

    private SearchType mSearchType = SearchType.COMPANY;

    public SearchView(Context context) {
        super(context);
        initView();
    }

    public SearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public SearchView(Context context, AttributeSet attrs,
                      int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        View view = inflate(getContext(), R.layout.view_search_layout, this);
        mSearchContent = (EditText) view.findViewById(R.id.search_content);
        mContentClearIcon = (ImageView) view.findViewById(R.id.search_content_clear_iv);
        mCancelBtn = (TextView) view.findViewById(R.id.cancel_search_btn);
        mSearchContentView = ViewUtils.findView(view, R.id.search_content_panel);

        addListenersViews();
    }

    private void addListenersViews() {

        mContentClearIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onClearContent();
                    onContentClear();
                }
            }
        });

        mSearchContent.addTextChangedListener(mTextWatcher);

        mSearchContent.setImeOptions(EditorInfo.IME_ACTION_DONE);
        mSearchContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (EditorInfo.IME_ACTION_DONE == actionId || (event != null &&
                        KeyEvent.KEYCODE_ENTER == event.getKeyCode())) {
                    preformSearch();
                    return true;
                }
                return false;
            }
        });

        mCancelBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCancelSearch();
                }
            }
        });

    }

    /**
     *
     */
    public void preformSearch() {
        if (mListener != null) {
            mListener.onStartSearch(mSearchType, mSearchContent.getText().toString());
        }
    }

    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (mListener != null) {
                mListener.onContentChange(mSearchType, s.toString());
            }

            notifyContentChange(s);
        }
    };

    /**
     * @param s
     */
    private void notifyContentChange(CharSequence s) {
        if (s.length() > 0) {
            mContentClearIcon.setVisibility(View.VISIBLE);
            mSearchContent.setSelection(s.length());
        } else {
            mContentClearIcon.setVisibility(View.GONE);
        }
    }

    /**
     * @param searchType
     */
    public void setSearchType(SearchSourcePager pager, SearchType searchType) {
        mSearchType = searchType;
        mSearchContent.setHint(pager.getTips());
    }

    public void setSearchContent(String content) {
        setSearchContent(content, true);
    }

    /**
     * @param content
     * @param notifyTextWatcher
     */
    public void setSearchContent(String content, boolean notifyTextWatcher) {

        if (!TextUtils.isEmpty(content)) {
            if (!notifyTextWatcher) {
                mSearchContent.removeTextChangedListener(mTextWatcher);
                mSearchContent.setText(content);
                mSearchContent.addTextChangedListener(mTextWatcher);
                notifyContentChange(content);
            } else {
                mSearchContent.setText(content);
            }
        }
    }

    public View getSearchContentView() {
        return mSearchContentView;
    }

    public String getSearchContent() {
        return mSearchContent.getText().toString();
    }

    private void onContentClear() {
        mSearchContent.setText("");

    }

    public void setOnSearchViewClickListener(OnSearchViewClickListener l) {
        mListener = l;
    }

    public interface OnSearchViewClickListener {
        void onSearchTypeChange(SearchType searchType, String content);

        void onContentChange(SearchType searchType, String content);

        void onClearContent();

        void onStartSearch(SearchType searchType, String content);

        void onCancelSearch();
    }

}